using System;
using System.Windows.Forms;
using Jbe.TestSuite.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Commands;
using Microsoft.Practices.CompositeUI.SmartParts;
using Jbe.TestSuite.Infrastructure.Interface.Constants;
using Jbe.CABExtension.WinForms;
using Jbe.TestSuite.LogViewer.Demo.Properties;
using Jbe.TestSuite.Help.Interface.Services;
using System.ComponentModel;
using Jbe.CABExtension.WinForms.Workspaces;
using Jbe.CABExtension.SmartPartInfos;
using System.Reflection;
using Jbe.TestSuite.Infrastructure.Interface.Services;

namespace Jbe.TestSuite.LogViewer.Demo
{
    public class ModuleController : WorkItemController
    {
        private IUIElementCreationService uiElementCreationService;
        private IDynamicHelp dynamicHelp;

        [ServiceDependency]
        public IUIElementCreationService UIElementCreationService
        {
            set { uiElementCreationService = value; }
        }

        [ServiceDependency(Required=false)]
        public IDynamicHelp DynamicHelp
        {
            set { dynamicHelp = value; }
        }

        public override void Run()
        {
            WorkItem.Activated += new EventHandler(WorkItemActivated);

            IToolBarItem toolBarItem = uiElementCreationService.CreateToolBarItem(
                "Log Demo", Resources.ReportEdit.ToBitmap());
            WorkItem.UIExtensionSites[UIExtensionSiteNames.MainToolbar].Add(toolBarItem);
            toolBarItem.Click += new EventHandler(LogDemoToolBarItemClick);
        }

        private void ShowLogDemo() 
        {
            IconSmartPartInfo dpInfo = new IconSmartPartInfo("Log Demo", "Log Demo");
            dpInfo.Icon = Resources.ReportEdit;
            LogDemo logDemo = WorkItem.SmartParts.AddNew<LogDemo>();
            logDemo.Disposed += new EventHandler(LogDemoDisposed);
            WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Show(logDemo, dpInfo);
        }

        #region Command and Event handler

        private void LogDemoToolBarItemClick(object sender, EventArgs e)
        {
            ShowLogDemo();
        }

        private void LogDemoDisposed(object sender, EventArgs e)
        {
            WorkItem.SmartParts.Remove(sender);
        }

        private void WorkItemActivated(object sender, EventArgs e)
        {
            if (dynamicHelp != null)
            {
                dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Help/DemoPage.htm");
            }
        }

        #endregion
    }
}
